package com.zhengsr.drawbroad;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.zhengsr.drawbroad.data.BaseData;
import com.zhengsr.drawbroad.state.BaseState;
import com.zhengsr.drawbroad.utils.CommandUtils;
import com.zhengsr.drawbroad.utils.ConstractUtils;

import java.util.List;

/**
 * Created by zhengshaorui on 2018/1/7.
 * csdn: http://blog.csdn.net/u011418943
 */

public class BaseDrawView extends View {
    private static final String TAG = "BaseDrawView";
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private BaseState mCurrentState;

    private Bitmap mEraserBitmap;
    private Rect mSrcRect;
    private Rect mDesRect;
    private int mEraserwidth;


    public BaseDrawView(Context context) {
        this(context,null);
    }

    public BaseDrawView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BaseDrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setClickable(true);



        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(dm);
        mBitmap = Bitmap.createBitmap(dm.widthPixels,dm.heightPixels,
                Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

        BitmapDrawable drawable = (BitmapDrawable) DrawBroadApplication.getContext().getResources().
                getDrawable(R.mipmap.eraser_move);
        mEraserBitmap = drawable.getBitmap();
        mSrcRect = new Rect(0,0,mEraserBitmap.getWidth(),mEraserBitmap.getHeight());
        mEraserwidth = ConstractUtils.ERASER_WIDTH / 2;

    }

    @Override
    protected void onDraw(Canvas canvas) {
       // super.onDraw(canvas);
        canvas.drawBitmap(mBitmap,0,0,null);
        if (mDesRect != null) {
            canvas.drawBitmap(mEraserBitmap, mSrcRect, mDesRect, null);
        }
    }

    public void setCurrentState(BaseState baseState){
        mCurrentState = baseState;
        mCurrentState.onDraw(mCanvas);
        if (mCurrentState.getMode() != ConstractUtils.ERASER){
            mDesRect = null;
            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mCurrentState.drawDown(event.getX(),event.getY());

                break;
            case MotionEvent.ACTION_MOVE:
                mCurrentState.drawMove(event.getX(),event.getY());

                if (mCurrentState.getMode() == ConstractUtils.ERASER){
                    float x = event.getX();
                    float y = event.getY();
                    RectF rectF = new RectF(x-mEraserwidth,y-mEraserwidth,mEraserwidth+x,y+mEraserwidth);
                    mDesRect = new Rect();
                    rectF.round(mDesRect);
                }else{
                    mDesRect = null;
                }

                break;
            case MotionEvent.ACTION_UP:
                mCurrentState.drawUp();
                break;
            default:
                break;
        }
        invalidate();
        return true;
    }

    public void undo(){
        if (!CommandUtils.getInstance().getBaseDatas().isEmpty()){
            clear();
            CommandUtils.getInstance().undo();
            reDraw();
        }
    }
    public void redo(){
        CommandUtils.getInstance().redo();
        reDraw();
    }

    private void reDraw() {
        List<BaseData> baseDatas = CommandUtils.getInstance().getBaseDatas();
        for (BaseData data : baseDatas) {
            mCanvas.drawPath(data.path,data.paint);
        }
        invalidate();
    }

    public void clear(){
       // mCurrentState.clear();
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mCanvas.drawPaint(paint);
        invalidate();
    }

    public void reset(){
        clear();
        CommandUtils.getInstance().clear();
    }

    public void setPaintColor(int color){
        if (mCurrentState != null){
            mCurrentState.setPaintColor(color);
        }
    }


}
